﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using ZWFrameWork;

namespace View
{
    public class View : AbstractView
    {
        public Dictionary<string, Transform> _G = new Dictionary<string, Transform>();

        protected void Print(object args)
        {
            Debug.Log(args);
        }

        public override void OnLoad()
        {
            if (_G == null)
            {
                _G = new Dictionary<string, Transform>();
            }
            else
            {
                _G.Clear();
            }

            Transform[] transformsArray = transform.GetComponentsInChildren<Transform>(true);
            if (transformsArray.Length <= 0)
            {
                ////GameUtilty.Error("当前transform下无节点");
                return;
            }

            foreach (Transform transform_ in transformsArray)
            {
                //名字空格校验
                if (transform_.name.Length != transform_.name.Trim().Length)
                {
                    ////GameUtilty.Error("请检查组件名字存在空格[{0}]", transform_.name);
                }

                string componentName = transform_.name.Trim();

                if (componentName.Equals("Text") || componentName.Equals("Text (Legacy)")
                                                 || componentName.Equals("Placeholder")
                                                 || componentName.Equals("Image")
                   )
                {
                    continue;
                }
                
                if (_G.ContainsKey(componentName))
                {
                    //确保自己的组件名字唯一
                    // ////GameUtilty.Error("当前有组件命名冲突{0}请检查", componentName);
                }
                else
                {
                    _G[componentName] = transform_;
                }
            }
        }

        public override void OnShow(params object[] args)
        {
            PopActivityManager.Instance().OnCheckViewIsPopStack(Context.gameObject.name);
        }

        public override void OnClose(params object[] args)
        {
            PopActivityManager.Instance().OnShowPopView();
        }

        // public void AddClickListener(string path, UnityAction call)
        // {
        //     AddClickListener(transform.Find(path), call);
        // }


        public void AddClickListener(Button button, UnityAction call, bool isselfsound = true)
        {
            button.onClick.RemoveAllListeners(); //先移除全部事件，防止重复绑定
            button.onClick.AddListener(() =>
            {
                if (isselfsound)
                {
                    AudioSystem.Instance.OnPlayBtn();   
                }
                if (call != null)
                {
                    call();
                }
            });
            EventTrigger buttonTrigger = button.GetComponent<EventTrigger>();
            if (buttonTrigger == null)
            {
                buttonTrigger = button.gameObject.AddComponent<EventTrigger>();
            }

            buttonTrigger.triggers = new List<EventTrigger.Entry>();
            EventTrigger.Entry pointerUp = new EventTrigger.Entry();
            pointerUp.eventID = EventTriggerType.PointerUp;
            pointerUp.callback.AddListener(
                (BaseEventData arg0) =>
                {
                    //Debug.LogError("抬起");
                    button.transform.localScale = new Vector3(1f, 1f, 1f);
                });
            EventTrigger.Entry pointerDown = new EventTrigger.Entry();
            pointerDown.eventID = EventTriggerType.PointerDown;
            pointerDown.callback.AddListener(
                (BaseEventData arg0) =>
                {
                    button.transform.localScale = new Vector3(0.95f, 0.95f, 1f);
                });
            buttonTrigger.triggers.Add(pointerUp);
            buttonTrigger.triggers.Add(pointerDown);
        }

        public void RemoveClickListener(Button button)
        {
            button.onClick.RemoveAllListeners();
        }
        
        /// <summary>
        /// 根据transform
        /// </summary>
        /// <param name="transform_"></param>

        public void RemoveClickListener(Transform transform_)
        {
            Button _button = null;
            if(transform_.TryGetComponent(out _button))
            {
                RemoveClickListener(_button);
            }
            else
            {
                ////GameUtilty.Error("{0}未找到button组件,移除失败");   
            }
        }
        
        public void AddClickListener(GameObject gameObject, UnityAction call)
        {
            if (!gameObject.TryGetComponent<Button>(out Button button))
            {
                button = gameObject.AddComponent<Button>();
            }
            
            AddClickListener(button, call);
        }

        public void AddClickListener(Transform transform, UnityAction call)
        {
            AddClickListener(transform.gameObject, call);
        }
        public void StartCoroutine(IEnumerator enumerator)
        {
            Utils.StartCoroutine(enumerator);
        }

        public void StopCoroutine(Coroutine coroutine)
        {
            Utils.StopCoroutine(coroutine);
        }

        public void StopCoroutine(string name)
        {
            Utils.StopCoroutine(name);
        }
    }
}